package com.zidian.study.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zidian.student.trucks.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by GongCheng on 2016/12/1.
 */

public class Top10ListAdapter extends RecyclerView.Adapter<ViewHolder> {

    private static final int TYPE_TOP = 1;
    private static final int TYPE_CENTER = 2 ;
    private static final int TYPE_BOTTOM = 3;

    private String headPortrait;
    private List<String> tags = new ArrayList<>();

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TOP) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top10_top, parent, false);
            return new TopViewHolder(view);
        }
        if (viewType == TYPE_CENTER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top10_center, parent, false);
            return new CenterViewHolder(view);
        }
        if (viewType == TYPE_BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top10_bottom, parent, false);
            return new BottomViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder instanceof TopViewHolder) {
            Glide.with(((TopViewHolder) holder).teacherImage.getContext())
                    .load(headPortrait)
                    .placeholder(R.drawable.teacher)
                    .into(((TopViewHolder) holder).teacherImage);
        }
        if (holder instanceof CenterViewHolder) {
            ((CenterViewHolder) holder).adapter.setTags(tags);
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        }
        if (position == 1) {
            return TYPE_CENTER;
        }
        if (position == 2) {
            return TYPE_BOTTOM;
        }
        return super.getItemViewType(position);
    }

    /**
     * 设置头像
     * @param headPortrait
     */
    public void setHeadPortrait(String headPortrait){
        this.headPortrait = headPortrait;
    }

    public void setTags(List<String> tags){
        this.tags = tags;
        notifyDataSetChanged();
    }

    static class TopViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.top10_teacher_image_iv)
        CircleImageView teacherImage;

        public TopViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class CenterViewHolder extends ViewHolder {
        @Bind(R.id.top_list_rv)
        RecyclerView topListRv;

        private TopListAdapter adapter;

        public CenterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            adapter = new TopListAdapter();
            topListRv.setLayoutManager(new LinearLayoutManager(topListRv.getContext()));
            topListRv.setAdapter(adapter);
        }
    }

    static class BottomViewHolder extends ViewHolder {
        public BottomViewHolder(View itemView) {
            super(itemView);
        }
    }
}
